import json
import requests
from bs4 import BeautifulSoup
base_url = "https://www.hbjcxy.com/jyyw/index"
for i in range(0,5):
    if i==0:
        url = base_url+".html"
        # 请求
        response = requests.get(url)
        response.encoding = "utf-8"
        # 解析网页
        soup = BeautifulSoup(response.text,features="html.parser")
        items = soup.select(".list-item")
        data_t = []
        # 循环
        for item in items:
            title = item.select(".list-title>a")[0].text.strip()
            date = item.select(".list-meta")[0].text.strip()
            d = {"标题":title,"时间":date}
            data_t.append(d)
        # 保存文件
        with open("新闻/"+str(i)+".json","w+",encoding="utf-8") as f:
            f.write(json.dumps(data_t,ensure_ascii=False,indent=2))
    else:
        url = base_url+"_"+str(i+1)+".html"
        # 请求
        response = requests.get(url)
        response.encoding = "utf-8"
        # 解析网页
        soup = BeautifulSoup(response.text,features="html.parser")
        items = soup.select(".list-item")
        data_t = []
        # 循环
        for item in items:
            title = item.select(".list-title>a")[0].text.strip()
            date = item.select(".list-meta")[0].text.strip()
            d = {"标题": title, "时间": date}
            data_t.append(d)
        # 保存文件
        with open("新闻/" + str(i) + ".json", "w+", encoding="utf-8") as f:
            f.write(json.dumps(data_t, ensure_ascii=False, indent=2))